package com.wxapp.common.constant.base;

import com.wxapp.common.base.util.jackson.JacksonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;

/**
 * @author lgl 监听基础类
 * @Description
 * @Datetime 2023/8/23 9:28
 * @Modified By
 */

public abstract class IBaseListen<T extends IBaseEvent<E>, E> implements ApplicationListener<T> {
    private static final Logger log = LoggerFactory.getLogger(IBaseListen.class);

    @Override
    @Async("WxExecutor")
    public void onApplicationEvent(T event) {
        log.info("事件监听器（异步） - 收到消息：{}，开始处理。", event.getMessage());
        try {
            var dealListen = toDealListen(event);
            if (dealListen != null) {
                log.info("执行返回结果：{}", JacksonUtil.toJSONString(dealListen));
            }
        } catch (Exception e) {
            log.info("事件监听器异常：{}", e.getMessage());
            throw new RuntimeException(e);
        }
        log.info("事件监听器（异步） - 处理完成！");
    }

    protected abstract  Object toDealListen(T event) throws Exception;
}
